package com.gtmc.mould.jsonstr;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.logging.Logger;
import com.gtmc.mould.impala.ImpalaSink;
import com.gtmc.mould.shell.ShellHelp;
/**
 * @author: MaLiang
 * @date: 2020/8/6 17:29
 * @version: 1.0
 * 解析json 数据
 **/
public class JsonArrResolving {
    Logger logger = Logger.getLogger(JsonArrResolving.class.getName());
    public  String jsonToObject(String jsonStr) throws Exception {

        JSONObject json = JSONObject.parseObject(jsonStr);
        //获取item，得到json数组
        JSONArray array = (JSONArray)json.get("data");
        //调用impala接口写入数据
        int i ;
        ImpalaSink impalaS = new ImpalaSink();
        logger.info("==========开始解析kafka数据");
        System.out.println("==========array.size():"+array.size());
        if(array.size()>=1){
            for (i = 0; i < array.size(); i++) {
                JSONObject jsobj = array.getJSONObject(i);
                String   key = jsobj.getString("key");
                String  tierOnePartNo = jsobj.getString("tierOnePartNo");
                String  partNo = jsobj.getString("partNo");
                Double  partLength = jsobj.getDouble("partLength");
                Double  partWidth = jsobj.getDouble("partWidth");
                Double  partHeight = jsobj.getDouble("partHeight");
                Double  partThickness = jsobj.getDouble("partThickness");
                String  partMaterial = jsobj.getString("partMaterial");
                String  modelTypeName = jsobj.getString("modelTypeName");
                String  mouldMaterial = jsobj.getString("mouldMaterial");
                Double  mouldLength = jsobj.getDouble("mouldLength");
                Double  mouldWidth = jsobj.getDouble("mouldWidth");
                Double  mouldHeight = jsobj.getDouble("mouldHeight");
                Double  mouldWeight = jsobj.getDouble("mouldWeight");
                int equipmentTonnage = jsobj.getInteger("equipmentTonnage");
                int takeNum = jsobj.getInteger("takeNum");
                String equipmentTypeName = jsobj.getString("equipmentTypeName");
                String processName = jsobj.getString("processName");
                int camNum = jsobj.getInteger("camNum");
                int mouldStepNum = jsobj.getInteger("mouldStepNum");
                Double emptyCoefficient = jsobj.getDouble("emptyCoefficient");
                String lifeNum_str = jsobj.getString("lifeNum");
                int lifeNum =0;
                if(lifeNum_str !=null){
                     lifeNum = Integer.valueOf(lifeNum_str);
                }
                String manufacturer = jsobj.getString("manufacturer");
                String unitPrice = jsobj.getString("unitPrice");
                String quoteTime = jsobj.getString("quoteTime");
                String supplier = jsobj.getString("supplier");
                String dataStatus = jsobj.getString("dataStatus");
                logger.info("==========将解析完的数据写入hive");
                impalaS.SinkImpala(key ,tierOnePartNo ,partNo ,partLength ,partWidth ,partHeight ,partThickness ,partMaterial ,modelTypeName ,mouldMaterial ,mouldLength ,mouldWidth ,mouldHeight ,mouldWeight ,equipmentTonnage ,takeNum ,equipmentTypeName ,processName ,camNum ,mouldStepNum ,emptyCoefficient ,lifeNum ,manufacturer ,unitPrice ,quoteTime ,supplier ,dataStatus );
            }
        }

        //这里远程执行 数据清洗脚本
        String sh_ip = "172.16.136.59";
        int sh_port =22;
        String sh_user = "dintm";
        String sh_pwd = "pass1234";
        logger.info("===============开始执行shell脚本进行数据清洗===============");
        ShellHelp shellHelp = new ShellHelp();
        shellHelp.init(sh_ip,sh_port,sh_user,sh_pwd);
        String sh_str = shellHelp.execCmd("/bin/sh /home/dintm/mould/shell/halabel_mould.sh");
        logger.info("shell命令返回值："+sh_str);
        logger.info("===============执行shell脚本进行数据清洗完成===============");
        //这里开始调用python 进行数据预测
        //这里远程执行 数据清洗脚本
        String py_ip = "172.24.57.17";
        int py_port =22;
        String py_user = "opt_dmp";
        String py_pwd = "GTMCdmp-202004";
        logger.info("===============开始执行python脚本进行数据预测===============");
        shellHelp.init(py_ip,py_port,py_user,py_pwd);

        String py_str = shellHelp.execCmd("cd /home/opt_dmp/mould/src/run;/bin/bash run-shell.sh");

        logger.info("===============shell命令返回值："+py_str);
        logger.info("===============执行python脚本进行数据预测完成===============");

        return "---------------数据写入完成---------------";
    }


}
